package com.edu.juc;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
//        while (in.hasNextInt()) { // 注意 while 处理多个 case
//            int a = in.nextInt();
//            int b = in.nextInt();
//            System.out.println(a + b);
//        }
        String input = "[(0,20],(20,40],(40,60],(60,99)]";
        System.out.println(m(input));

    }

    public static String m(String input) {
        ArrayList<int[]> list = new ArrayList<>();

        String[] parts = input.replaceAll("[\\[\\]()]", "").split(",\\s*");

        for (String part :
                parts) {
            String[] numbers = part.split(",");
            if (numbers.length != 2) {
                continue;
            }

            try {
                int start = Integer.parseInt(numbers[0].trim());
                int end = Integer.parseInt(numbers[1].trim());
                list.add(new int[]{start, end});
            } catch (NumberFormatException e) {
                return "false";
            }
        }

        list.sort(Comparator.comparingInt(a -> a[0]));

        if (list.isEmpty())
            return "false";

        if (list.get(0)[0] != 0) {
            list.get(0)[0] = 0;
        }

        int n = list.size();

        if (list.get(n - 1)[1] != 99) {
            list.get(n - 1)[1] = 99;
        }
        for (int i = 0; i < n; i++) {
            int prevEnd = list.get(i - 1)[1];
            int currStart = list.get(i)[0];

            if (currStart != prevEnd) {
                list.get(i)[0] = prevEnd;
            }
        }

        StringBuilder result = new StringBuilder("[");
        for (int i = 0; i < n; i++) {
            int start = list.get(i)[0];
            int end = list.get(i)[1];

            result.append("(").append(start).append(",").append(end).append("]");
            if (i < n - 1) {
                result.append(",");
            }
        }

        result.append("]");
        return result.toString();


    }
}
